/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdScheduler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fujitsu.nanjing.tieba.base.BaseController;

/**
 * TriggerController.java
 * 
 * @author Administrator
 */
@Controller
public class TriggerController extends BaseController {
	@Resource
	private StdScheduler scheduler;
	@PreAuthorize("hasRole('管理员')")
	@RequestMapping(value = {"/trigger", "/admin"}, method = RequestMethod.GET)
	public String trigger(Model model) throws SchedulerException {
		List<TriggerStruct> triggers = new ArrayList<TriggerStruct>();
		for (String groupName : scheduler.getTriggerGroupNames()) {
			TriggerStruct triggerStruct = null;
			String[] triggerNames = scheduler.getTriggerNames(groupName);
			for (String triggerName : triggerNames) {
				triggerStruct = new TriggerStruct();
				triggerStruct.setTriggerGroup(groupName);
				triggerStruct.setTriggerName(triggerName);
				triggers.add(triggerStruct);
			}
		}
		model.addAttribute("triggers", triggers);
		return "trigger/trigger";
	}
	@PreAuthorize("hasRole('管理员')")
	@RequestMapping("/trigger/{triggerGroup}/{triggerName}/{operation}")
	public String trigger(Model model, @PathVariable String triggerGroup,
			@PathVariable String triggerName, @PathVariable String operation)
			throws SchedulerException, InstantiationException,
			IllegalAccessException {
		Trigger trigger = scheduler.getTrigger(triggerName, triggerGroup);
		if (trigger != null) {
			if (operation != null) {
				if (operation.equals("enable")) {
					scheduler.resumeJob(trigger.getJobName(),
							trigger.getJobGroup());
				} else if (operation.equals("disable")) {
					scheduler.pauseJob(trigger.getJobName(),
							trigger.getJobGroup());
				} else {
					/*
					 * JobDetail jobDetail = scheduler.getJobDetail(
					 * trigger.getJobName(), trigger.getJobGroup()); Job job =
					 * (Job) jobDetail.getJobClass().newInstance();
					 */
					scheduler.triggerJob(trigger.getJobName(),
							trigger.getJobGroup());
				}
			}

		}
		return redirect("/trigger");
	}
	public class TriggerStruct {
		private String triggerName;
		private String triggerGroup;
		public String getTriggerName() {
			return triggerName;
		}
		public void setTriggerName(String triggerName) {
			this.triggerName = triggerName;
		}
		public String getTriggerGroup() {
			return triggerGroup;
		}
		public void setTriggerGroup(String triggerGroup) {
			this.triggerGroup = triggerGroup;
		}
	}

}
